/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import application.Facade;
import application.beans.CompteBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Étienne
 */
public class ServSauvegarderComptes extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        Facade f = new Facade();
        
        String next = ConstantesWeb.ATTRIBUT_PAGE_GESTIONCOMPTE;
        List<CompteBean> resultats = null;
        CompteBean temp = null;
        String elem = "";
        
        // On passe à travers les comptes affichés et on enregistre les modifications
        for(Enumeration e = request.getParameterNames(); e.hasMoreElements();){
            elem = (String)e.nextElement();
            if(elem.indexOf("identifiant") > -1){
                temp = new CompteBean();
                // On va chercher l'id du compte
                int identifiant = Integer.parseInt(request.getParameter(elem));
                temp.setIdCompte(identifiant);
                temp.setUsername(f.decoderChaine(request.getParameter("username" + identifiant)));
                temp.setPrenom(f.decoderChaine(request.getParameter("prenom" + identifiant)));
                temp.setNom(f.decoderChaine(request.getParameter("nom" + identifiant)));
                temp.setCodePermanent(request.getParameter("code" + identifiant));
                temp.setSexe(request.getParameter("sexe" + identifiant));
                temp.setCourriel(request.getParameter("courriel" + identifiant));
                temp.setDomaineEtude(f.decoderChaine(request.getParameter("domaine" + identifiant)));
                temp.setStatut(f.decoderChaine(request.getParameter("statut" + identifiant)));
                temp.setActif(request.getParameter("actif" + identifiant));
                
                f.miseAJourCompte(temp);
            }
        }

        // On va chercher les résultats mis à jour de la recherche
        resultats = f.rechercheComptesAvancee((CompteBean)session.getAttribute(ConstantesWeb.ATTRIBUT_CRITERES_GESTION_COMPTE));
        session.setAttribute(ConstantesWeb.ATTRIBUT_RESULTAT_GESTION_COMPTE, resultats);
        
        RequestDispatcher rd = getServletContext().getRequestDispatcher(next);
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
